package com.xone.xapp;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class AbstractListAdapter<T> extends BaseAdapter {
	
	protected List<T> list;
	protected LayoutInflater mInflater;
	protected int resource;
	protected Context context;
	
	public AbstractListAdapter(Context context, List<T> list, int resource) {
		this.context = context;
		this.list = list;
		this.resource = resource;
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}


	@Override
	public int getCount() {
		if (null == list || list.isEmpty()) {
			return 0;
		} 
		return list.size();
	}

	@Override
	public T getItem(int position) {
		if (position >= getCount()) {
			return null;
		}
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (null == list || list.isEmpty()) {
			return convertView;
		}
        View v;
        if (convertView == null) {
            v = mInflater.inflate(resource, parent, false);
        } else {
            v = convertView;
        }
        v = viewHandler(position, v, parent);
		return v;
	}
	
	public abstract View viewHandler(int position, View convertView, ViewGroup parent);

}
